const Service = require("egg").Service;

class Bookservice extends Service {

    async getBookList() {
        try {
            const bookList = await this.app.model.Book.findAll({})
            // console.log("哈哈哈哈哈哈哈哈：",bookList);
            return bookList
        } catch (error) {
            return null
        }
    }
    
    // 添加书籍（添加章节）
    async createBook(body) {
        try {
            const book = {
                title: body.title,
                md_text: body.md_text,
                html_text: body.html_text,
            }
            await this.app.model.Book.create(book)
            return true;
        } catch (error) {
            return false;
        }
    }
    
    
    async updateBook(id, {title,md_text,html_text}) {
        try {
            await this.app.model.Book.update({
                title,
                md_text,
                html_text
            }, {
                where: {
                    id
                }
            })
            return true
        } catch (error) {
            return false;;

        }
    }
    async deleteBook(id) {
        try {
            this.app.model.Book.destroy({
                where:{id}
            }
        )
            return true
        } catch (error) {
            return false;;

        }
    }


    async getBookDetail(id) {
        try {
            const book = await this.app.model.Chapter.findOne({
                where: {
                    id
                }
            })
            return book
        } catch (error) {
            return null
        }
    }
} 
module.exports = Bookservice;